<?php

namespace app\api\controller;

use app\admin\model\Comment as CommentModel;

class Comment extends Controller
{


//
//    public function index(){
////        var_dump(session('username'));
//
//        session_start();
//        $name = $_SESSION['n'] ;
//        $model = new CommentModel();
//        $users = $model->getList($name);
//        $this->assign('comment_id', $users);
//        return $this->renderSuccess($users, '获取数据成功');
//
//    }

//    public function detail($id)
//    {
//        $model = new WearModel();
//        $emp = $model->where(['wear_id' => $id])->find();
//        return $this->renderSuccess($emp, '获取员工数据成功');
//    }



    public function index()//显示一条动态的所有评论  $no是此条动态的编号
    {
        $model = new CommentModel();
        //查询出用户名，头像，评论内容，时间
        //*多表查询
        //以food_comment为主表
        $emps = db('wear_comment')
            ->alias("a")//取一个别名
        //查询此条美食动态的评论
            //与user表进行关联，取名i，并且a表的user_id字段等于user表的user_id字段
            ->join('user i', 'a.user_id = i.user_id')
            //想要的字段
            ->field('i.username,i.image,a.text,a.time')
            //查询
            ->select();
        return $this->renderSuccess($emps, '获取穿搭评论数据成功');


//        $model = new CommentModel();
//        if ($model->save($this->request->param())) {
//            $new_emp = $model->where(['comment_id' => $model])->find();
//            return $this->renderSuccess($new_emp, '添加成功');
//        } else {
//            return $this->renderError('添加失败');
//        }



    }
//
    public function wearcommentadd()
    {
        $data = $this->request->param();

        $model = new CommentModel();
        //前端提交表单中已收到评论的一条美食编号
//        $model->user_id = $_SESSION['n'];
        session_start();
        $name = $_SESSION['n'] ;

        $model->time = date("Y-m-d H:i:s");

        $res = $model->save($data);//保存进数据库

        if ($res) {//测试可注释
            return $this->renderSuccess($res, '评论成功');
        } else {
            return $this->renderError('评论失败');
        }
    }


}

